package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.ItemDTO;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    /**
     * 分页查询商品信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam(name = "page")Integer page,
                              @RequestParam(name = "size")Integer size){
//        分页条件
        Page<Item> ipage = new Page<>(page,size);
//        查询条件
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper();
//        不查询逻辑删除的数据   status != 3
        queryWrapper.ne(Item::getStatus,3);
//        根据id 倒叙
        queryWrapper.orderByDesc(Item::getId);

        Page<Item> itemPage = itemService.page(ipage,queryWrapper);

        return new PageDTO<>(itemPage.getTotal(),itemPage.getRecords());
    }

    /**
     * 分页查询商品信息 为搜索服务
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findByPage")
    public PageDTO<ItemDTO> findByPage4Search(
            @RequestParam(name = "page")Integer page,
            @RequestParam(name = "size")Integer size){
//        分页条件
        Page<Item> ipage = new Page<>(page,size);
//        查询条件
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper();
//        只查询 正常数据
        queryWrapper.eq(Item::getStatus,1);
//        根据id 倒叙
        queryWrapper.orderByAsc(Item::getId);

        Page<Item> itemPage = itemService.page(ipage,queryWrapper);

        List<Item> itemList = itemPage.getRecords();

//        对象转换
        List<ItemDTO> itemDTOList = new ArrayList<>();
        for (Item item : itemList) {
            ItemDTO itemDTO = new ItemDTO();
            BeanUtils.copyProperties(item,itemDTO);
            itemDTOList.add(itemDTO);
        }

        return new PageDTO<ItemDTO>(itemPage.getTotal(),itemDTOList);
    }

    /**
     * 根据主键id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item queryById(@PathVariable(name = "id")Long id){
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 根据主键id查询
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public ItemDTO getById(@PathVariable(name = "id")Long id){
        Item item = itemService.getById(id);
        ItemDTO itemDTO = new ItemDTO();
        BeanUtils.copyProperties(item,itemDTO);
        return itemDTO;
    }

    /**
     * 新增
     * @param item
     */
    @PostMapping
    public void save(@RequestBody Item item){

        if(StringUtils.isEmpty(item.getName())){
            throw new RuntimeException("名称不能为空");
        }
        item.setStatus(1);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 上下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void updateDownOrUp(@PathVariable(name = "id") Long id,
                               @PathVariable(name = "status")Integer status){

        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        item.setUpdateTime(new Date());
        itemService.updateById(item);


        String  routingKey = status==1?"item.up":"item.down";

        rabbitTemplate.convertAndSend("item.topic",routingKey,id.toString());

    }

    @PutMapping
    public void update(@RequestBody Item item){

        item.setStatus(null);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
    }

    /**
     * 逻辑删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable(name = "id")Long id){

//        物理删除
//        itemService.removeById(id);
//        逻辑删除，修改status状态 改为3
        Item item = new Item();
        item.setId(id);
        item.setStatus(3);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
    }


    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable(name = "itemId") Long itemId,
                            @PathVariable(name = "num") Integer num){
        itemService.deductStock(itemId,num);
    }

    @PutMapping("/restoreStock/{itemId}/{num}")
    public void restoreStock(@PathVariable(name = "itemId") Long itemId,
                            @PathVariable(name = "num") Integer num){
        itemService.restoreStock(itemId,num);
    }
}
